package com.thlws.rabbitmq.routing;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/***
 * 路由模式
 * BuiltinExchangeType.DIRECT
 * 本质是是消费端对 routingKey 的过滤
 *
 */
public class Producer {

	private static final String EXCHANGE_NAME = "direct_logs";

	public static void main(String[] args) throws Exception {

		ConnectionFactory factory = new ConnectionFactory();
		factory.setHost("116.228.212.36");
		factory.setUsername("hanley");
		factory.setPassword("hanley");
		factory.setVirtualHost("/test");

		try (Connection connection = factory.newConnection();
				Channel channel = connection.createChannel()) {

			// BuiltinExchangeType.DIRECT 表示启用路由模式
			channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);

			String routingKey = "info";
			String message = "路由模式,测试信息";

			channel.basicPublish(EXCHANGE_NAME, routingKey, null, message.getBytes("UTF-8"));
			System.out.println(" [x] Sent '" + routingKey + "':'" + message + "'");
		}
	}

}
